package com.example.redisspring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Set;

@RestController
@RequestMapping("/string")
public class MyController {
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @RequestMapping("/get")
    public String get(){
        stringRedisTemplate.opsForValue().set("key", "val");
        String val = stringRedisTemplate.opsForValue().get("key");
        return val;
    }
    @RequestMapping("/list")
    public String flush(){
        // execute 中的操作可能会有返回值，所以需要 return
        stringRedisTemplate.execute((RedisConnection connection) -> {
            connection.flushDb();
            return null;
        });
        stringRedisTemplate.opsForList().leftPush("key", "1");
        stringRedisTemplate.opsForList().leftPush("key", "2");
        stringRedisTemplate.opsForList().leftPush("key", "3");
        String a = stringRedisTemplate.opsForList().rightPop("key");
        String b = stringRedisTemplate.opsForList().rightPop("key");
        String c = stringRedisTemplate.opsForList().rightPop("key");
        return a + b + c;
    }
    @RequestMapping("/set")
    public String set(){
        stringRedisTemplate.execute((RedisConnection connection) -> {
            connection.flushDb();
            return null;
        });
        stringRedisTemplate.opsForSet().add("key", "111", "222", "333");
        Set<String> key = stringRedisTemplate.opsForSet().members("key");
        System.out.println(key);

        Boolean key1 = stringRedisTemplate.opsForSet().isMember("key", "111");
        System.out.println(key1);

        stringRedisTemplate.opsForSet().remove("key", "222");
        Set<String> key2 = stringRedisTemplate.opsForSet().members("key");
        System.out.println(key2);
        return "ok";
    }

    @RequestMapping("/hash")
    public String hash(){
        stringRedisTemplate.execute((RedisConnection connect) -> {
            connect.flushDb();
            return null;
        });
        stringRedisTemplate.opsForHash().put("key", "f1", "1");
        stringRedisTemplate.opsForHash().put("key", "f2", "2");
        stringRedisTemplate.opsForHash().put("key", "f3", "3");
        stringRedisTemplate.opsForHash().put("key", "f4", "4");
        String res = (String) stringRedisTemplate.opsForHash().get("key", "f2");
        System.out.println(res);

        Boolean b = stringRedisTemplate.opsForHash().hasKey("key", "f1");
        System.out.println(b);

        Long delete = stringRedisTemplate.opsForHash().delete("key", "f1", "f2");
        System.out.println(delete);

        Long key = stringRedisTemplate.opsForHash().size("key");
        System.out.println(key);
        return "ok";
    }
    @RequestMapping("/zset")
    public String zset(){
        stringRedisTemplate.execute((RedisConnection connect) -> {
            connect.flushDb();
            return null;
        });
        stringRedisTemplate.opsForZSet().add("key", "zhangsan", 10);
        stringRedisTemplate.opsForZSet().add("key", "lisi", 1);
        stringRedisTemplate.opsForZSet().add("key", "wangwu", 12);
        Set<String> key = stringRedisTemplate.opsForZSet().range("key", 0, -1);
        System.out.println(key);

        Set<ZSetOperations.TypedTuple<String>> key1 = stringRedisTemplate.opsForZSet().rangeWithScores("key", 0, -1);
        System.out.println(key1);

        Long size = stringRedisTemplate.opsForZSet().size("key");
        System.out.println(size);

        Double score = stringRedisTemplate.opsForZSet().score("key", "lisi");
        System.out.println(score);

        return "ok";
    }
}
